package com.parking.stall.service.call;

import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

@Component
public class StringDispose {

    //数字字符串排序
    public static String numStringSort (String numString) {
        String[] stringArray = numString.split(",");
        int[] intArray = new int[stringArray.length];
        for (int i = 0; i < stringArray.length; i++) {
            intArray[i] = Integer.parseInt(stringArray[i]);
        }
        // Step 2: 对数字数组进行排序
        Arrays.sort(intArray);
        // Step 3: 将排序后的数字数组转换回字符串形式
        StringBuilder sortedString = new StringBuilder();
        for (int i = 0; i < intArray.length; i++) {
            sortedString.append(intArray[i]);
            if (i < intArray.length - 1) {
                sortedString.append(",");
            }
        }
        return sortedString.toString();
    }

    //获取重复字符串下标的集合
    public static List<Integer> getRepetitionIndex (String num) {
        String[] numbers = num.split(",");
        int[] sortedNumbers = new int[numbers.length];
        for (int i = 0; i < numbers.length; i++) {
            sortedNumbers[i] = Integer.parseInt(numbers[i]);
        }
        Arrays.sort(sortedNumbers);

        List<Integer> numIndex = new ArrayList<>();
        for (int i = 1; i < sortedNumbers.length; i++) {
            if (sortedNumbers[i] == sortedNumbers[i - 1]) {
                numIndex.add(i);
                numIndex.add(i+1);
            }
        }
        System.out.println("重复数字下标：" + numIndex);
        return numIndex;
    }

    //获取去重（包含已重复数字）后的字符串
    public static String getNumStringDeduplication(String numString,List<Integer> nums){
        List<Integer> numsNew = new ArrayList<>();
        for (Integer num : nums) {
            num=num-1;
            numsNew.add(num);
        }
        String[] numbers = numString.split(",");
        StringBuilder resultBuilder = new StringBuilder();
        for (int i = 0; i < numbers.length; i++) {
            if (!contains(numsNew, i)) {
                resultBuilder.append(numbers[i]);
                if (i != numbers.length - 1) {
                    resultBuilder.append(",");
                }
            }
        }
        String newString = resultBuilder.toString();
        System.out.println("去重后的数字："+newString);
        return newString;
    }

    private static boolean contains(List<Integer> nums, int key) {
        for (int i : nums) {
            if (i == key) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) throws Exception{
//        String originalString = "1,12,9,9,5,5,7";
//        String originalString = "1,4,4,5,5,7,10,11,11,12,15,17";
//        List<Integer> nums = new ArrayList<>();
//        nums.add(2);nums.add(3);nums.add(4);nums.add(5);nums.add(9);nums.add(10);
//        getNumStringDeduplication(originalString,nums);
//        getRepetitionIndex(originalString);

        // Step 1: 将字符串拆分为数字数组
//        String sortedString = numStringSort(originalString);
//        System.out.println("排序后的字符串：" + sortedString);
     }
}
